package k.b.s;

import k.b.s.model.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class ItemXmlHelper {
	private static ItemXmlHelper xmlHelper = new ItemXmlHelper();

	/**
	 * This class is singleton.
	 */
	private ItemXmlHelper() {
	}

	/**
	 * インスタンスの取得。
	 *
	 * @return このクラスのインスタンス
	 */
	public static ItemXmlHelper getInstance() {

		return xmlHelper;
	}



	public ArrayList<ItemRowModel> parseSax(String xml,String http) {
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(xml.getBytes());
			return parseSax(is,http);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}


	public ArrayList<ItemRowModel> parseSax(InputStream xml,String http){
		SAXParserFactory saxParaser = SAXParserFactory.newInstance();
		SaxHandler sh = null;
		try{
			SAXParser sp = saxParaser.newSAXParser();
			sh = new SaxHandler(http);
			sp.parse(xml,sh);

		}catch(Exception e){
			e.printStackTrace();
		}
		return sh.rowModelArray;
	}


	class SaxHandler extends DefaultHandler {
		ArrayList<ItemRowModel> rowModelArray = new ArrayList<ItemRowModel>();

		boolean isChannel;
		boolean isItem;
		boolean isDescription = false;
		boolean isTitle = false;
		boolean isPubdate = false;
		String channelLink = null;
		String description = null;
		String title = null;
		String pubDate = null;
		String http;

		public SaxHandler(String http) {
			super();
			this.http = http;
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {

			if ("channel".equals(localName)) {
				isChannel = true;
			}
			if (isChannel) {

				if("item".equals(localName)){
					isItem = true;
				}
				if(isItem&&isChannel){
					if ("title".equals(localName)){
						isTitle = true;
					}
					if ("description".equals(localName)){
						isDescription = true;
					}
					if ("pubDate".equals(localName)){
						isPubdate = true;
					}
				}

			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if("item".equals(localName)){
				ItemRowModel rowModel = new ItemRowModel();
				rowModel.setChannel(http);
				rowModel.setTitle(title);
				rowModel.setDescription(description);
				rowModel.setPubdate(java.util.Date.parse(pubDate));

				rowModelArray.add(rowModel);

				description = null;
				title = null;
				isItem = false;


			}

			if ("channel".equals(localName)) {

				isChannel = false;
			}
		}




		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (isChannel) {

				if (isTitle) {
					title = new String(ch,start,length);
					isTitle = false;
				}
				if (isDescription) {
					description = new String(ch, start, length);
					isDescription = false;
				}
				if (isPubdate) {
					pubDate = new String(ch, start, length);
					isPubdate = false;
				}
			}
		}
	}
}
